// 主菜单场景
class MainMenu extends Phaser.Scene {
    constructor() {
        super({ key: 'MainMenu' });
    }

    preload() {
        // 创建简单的背景
        this.add.graphics()
            .fillStyle(0x0a0a2c)
            .fillRect(0, 0, 800, 600)
            .generateTexture('menu_bg', 800, 600);
            
        // 创建按钮背景
        this.add.graphics()
            .fillStyle(0x3498db)
            .fillRect(0, 0, 200, 50)
            .lineStyle(2, 0xffffff)
            .strokeRect(0, 0, 200, 50)
            .generateTexture('button_bg', 200, 50);
    }

    create() {
        // 添加背景
        this.add.image(400, 300, 'menu_bg');
        
        // 游戏标题
        this.add.text(400, 150, '像素生存者', {
            fontSize: '64px',
            fontFamily: 'Arial',
            fill: '#ffffff',
            stroke: '#000000',
            strokeThickness: 6
        }).setOrigin(0.5);
        
        // 副标题
        this.add.text(400, 220, 'Pixel Survivor', {
            fontSize: '32px',
            fontFamily: 'Arial',
            fill: '#aaaaff'
        }).setOrigin(0.5);
        
        // 开始游戏按钮
        const startButton = this.add.image(400, 320, 'button_bg').setOrigin(0.5);
        const startText = this.add.text(400, 320, '开始游戏', {
            fontSize: '24px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        // 成就按钮
        const achievementButton = this.add.image(400, 390, 'button_bg').setOrigin(0.5);
        const achievementText = this.add.text(400, 390, '成就', {
            fontSize: '24px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        // 设置按钮
        const settingsButton = this.add.image(400, 460, 'button_bg').setOrigin(0.5);
        const settingsText = this.add.text(400, 460, '设置', {
            fontSize: '24px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        // 添加交互
        startButton.setInteractive();
        achievementButton.setInteractive();
        settingsButton.setInteractive();
        
        // 按钮悬停效果
        this.setupButtonHover(startButton);
        this.setupButtonHover(achievementButton);
        this.setupButtonHover(settingsButton);
        
        // 按钮点击事件
        startButton.on('pointerdown', () => {
            this.scene.start('PixelSurvivor');
        });
        
        achievementButton.on('pointerdown', () => {
            this.scene.start('Achievements');
        });
        
        settingsButton.on('pointerdown', () => {
            this.scene.start('Settings');
        });
        
        // 版本信息
        this.add.text(400, 550, 'v1.0.0', {
            fontSize: '16px',
            fontFamily: 'Arial',
            fill: '#888888'
        }).setOrigin(0.5);
        
        // 创建粒子效果
        // 先创建粒子纹理
        this.add.graphics()
            .fillStyle(0xffffff)
            .fillCircle(4, 4, 4)
            .generateTexture('spark', 8, 8);
            
        this.particles = this.add.particles(0, 0, 'spark', {
            x: 400,
            y: 150,
            speed: { min: 20, max: 50 },
            angle: { min: 0, max: 360 },
            scale: { start: 0.5, end: 0 },
            lifespan: 3000,
            quantity: 1,
            frequency: 100
        });
    }
    
    setupButtonHover(button) {
        button.on('pointerover', () => {
            button.setScale(1.1);
        });
        
        button.on('pointerout', () => {
            button.setScale(1.0);
        });
    }
}

// 成就场景
class Achievements extends Phaser.Scene {
    constructor() {
        super({ key: 'Achievements' });
        
        // 成就列表
        this.achievementList = [
            { id: 'wave5', name: '初级生存者', description: '存活5波', unlocked: false },
            { id: 'wave10', name: '中级生存者', description: '存活10波', unlocked: false },
            { id: 'wave20', name: '高级生存者', description: '存活20波', unlocked: false },
            { id: 'boss1', name: 'Boss猎人', description: '击败第一个Boss', unlocked: false },
            { id: 'boss3', name: 'Boss终结者', description: '击败3个Boss', unlocked: false },
            { id: 'allWeapons', name: '武器收藏家', description: '获得所有武器', unlocked: false },
            { id: 'kills100', name: '百人斩', description: '击杀100个敌人', unlocked: false },
            { id: 'kills500', name: '屠夫', description: '击杀500个敌人', unlocked: false },
            { id: 'exp1000', name: '经验丰富', description: '收集1000点经验', unlocked: false }
        ];
    }
    
    preload() {
        // 创建成就图标
        this.add.graphics()
            .fillStyle(0xffd700)
            .fillCircle(16, 16, 12)
            .generateTexture('achievement_icon', 32, 32);
            
        // 创建锁定图标
        this.add.graphics()
            .fillStyle(0x888888)
            .fillRect(8, 8, 16, 16)
            .lineStyle(2, 0x666666)
            .strokeRect(8, 8, 16, 16)
            .fillRect(14, 4, 4, 8)
            .generateTexture('locked_icon', 32, 32);
    }
    
    create() {
        // 添加背景
        this.add.image(400, 300, 'menu_bg');
        
        // 标题
        this.add.text(400, 50, '成就', {
            fontSize: '48px',
            fontFamily: 'Arial',
            fill: '#ffffff',
            stroke: '#000000',
            strokeThickness: 4
        }).setOrigin(0.5);
        
        // 加载成就数据
        this.loadAchievements();
        
        // 显示成就列表
        this.displayAchievements();
        
        // 返回按钮
        const backButton = this.add.image(400, 530, 'button_bg').setOrigin(0.5);
        const backText = this.add.text(400, 530, '返回', {
            fontSize: '24px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        backButton.setInteractive();
        backButton.on('pointerover', () => backButton.setScale(1.1));
        backButton.on('pointerout', () => backButton.setScale(1.0));
        backButton.on('pointerdown', () => {
            this.scene.start('MainMenu');
        });
    }
    
    loadAchievements() {
        // 从本地存储加载成就数据
        const savedData = localStorage.getItem('pixelSurvivorAchievements');
        if (savedData) {
            const parsedData = JSON.parse(savedData);
            this.achievementList.forEach(achievement => {
                if (parsedData[achievement.id]) {
                    achievement.unlocked = true;
                }
            });
        }
    }
    
    displayAchievements() {
        // 显示成就列表
        this.achievementList.forEach((achievement, index) => {
            const y = 120 + (index * 45);
            
            // 图标
            const icon = achievement.unlocked ? 'achievement_icon' : 'locked_icon';
            this.add.image(100, y, icon);
            
            // 成就名称
            const nameColor = achievement.unlocked ? '#ffffff' : '#888888';
            this.add.text(130, y - 10, achievement.name, {
                fontSize: '20px',
                fontFamily: 'Arial',
                fill: nameColor
            }).setOrigin(0, 0.5);
            
            // 成就描述
            const descColor = achievement.unlocked ? '#aaaaff' : '#666666';
            this.add.text(130, y + 10, achievement.description, {
                fontSize: '16px',
                fontFamily: 'Arial',
                fill: descColor
            }).setOrigin(0, 0.5);
        });
    }
}

// 设置场景
class Settings extends Phaser.Scene {
    constructor() {
        super({ key: 'Settings' });
    }
    
    create() {
        // 添加背景
        this.add.image(400, 300, 'menu_bg');
        
        // 标题
        this.add.text(400, 50, '设置', {
            fontSize: '48px',
            fontFamily: 'Arial',
            fill: '#ffffff',
            stroke: '#000000',
            strokeThickness: 4
        }).setOrigin(0.5);
        
        // 设置选项
        this.add.text(400, 150, '游戏难度', {
            fontSize: '24px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        // 难度选择按钮
        const easyButton = this.createDifficultyButton(250, 200, '简单', 0x4CAF50);
        const normalButton = this.createDifficultyButton(400, 200, '普通', 0x2196F3);
        const hardButton = this.createDifficultyButton(550, 200, '困难', 0xF44336);
        
        // 返回按钮
        const backButton = this.add.image(400, 500, 'button_bg').setOrigin(0.5);
        const backText = this.add.text(400, 500, '返回', {
            fontSize: '24px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        backButton.setInteractive();
        backButton.on('pointerover', () => backButton.setScale(1.1));
        backButton.on('pointerout', () => backButton.setScale(1.0));
        backButton.on('pointerdown', () => {
            this.scene.start('MainMenu');
        });
    }
    
    createDifficultyButton(x, y, text, color) {
        // 创建难度按钮
        const button = this.add.rectangle(x, y, 120, 40, color, 0.8)
            .setStrokeStyle(2, 0xffffff);
            
        const buttonText = this.add.text(x, y, text, {
            fontSize: '20px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
        
        button.setInteractive();
        
        button.on('pointerover', () => button.setScale(1.1));
        button.on('pointerout', () => button.setScale(1.0));
        button.on('pointerdown', () => {
            // 设置游戏难度
            localStorage.setItem('pixelSurvivorDifficulty', text);
            
            // 显示选择提示
            this.showSelectedText(x, y + 30, '已选择');
        });
        
        return button;
    }
    
    showSelectedText(x, y, text) {
        // 移除之前的选择提示
        if (this.selectedText) {
            this.selectedText.destroy();
        }
        
        // 显示新的选择提示
        this.selectedText = this.add.text(x, y, text, {
            fontSize: '16px',
            fontFamily: 'Arial',
            fill: '#ffffff'
        }).setOrigin(0.5);
    }
}